/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.designPatterns.observer;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
/**
 *
 * @author minga
 */
public class WeatherStation implements weatherSubject{
    Set<WeatherObserver> weatherObservers;
    int temperature;
    public WeatherStation ( int temperature ) 
    {
     weatherObservers = new HashSet<WeatherObserver >() ;
      this.temperature=temperature ;
    }
    @Override
    public void addObserver(WeatherObserver weatherObserver) {
        weatherObservers = new HashSet<WeatherObserver>() ;
        this.temperature = temperature;
    }

    @Override
    public void removeObserver(WeatherObserver weatherObserver) {
       weatherObservers.add(weatherObserver);
    }

    @Override
    public void doNotify() {
        Iterator<WeatherObserver > it = weatherObservers.iterator () ;
        while( it.hasNext()) 
        {
          WeatherObserver weatherObserver = it.next();
          weatherObserver.doUpdate ( temperature ) ;
        }
    }
    public void setTemperature ( int newTemperature ) 
    {
     System.out.println ( "nnWeather s t a t i o n s e t t i n g temperature to " + newTemperature ) ;
     temperature = newTemperature ;
     doNotify() ;
    }
}
